<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>{{id?'编辑':'添加'}}禁言</legend>
        </fieldset>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">区服</label>
                <div class="layui-input-block">
                    <select name="serverId" class="layui-select" :disabled="Id?true:false">
                        <option v-for="(item,index) in services" :value="item.server_id"
                                :selected="(!id && index==0) || (id && serverId==item.server_id)">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色ID</label>
                <div class="layui-input-block">
                    <input type="number" name="roleId" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" :disabled="Id?true:false">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">禁言时长(秒)</label>
                <div class="layui-input-block">
                    <input v-model="disableTime" type="number" name="disableTime" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                    <div class="quick-tags">
                        <button class="quick-tag" type="button" @click="setPresetValue(3600)">一小时</button>
                        <button class="quick-tag" type="button" @click="setPresetValue(10800)">3小时</button>
                        <button class="quick-tag" type="button" @click="setPresetValue(86400)">一天</button>
                        <button class="quick-tag" type="button" @click="setPresetValue(259200)">三天</button>
                        <button class="quick-tag" type="button" @click="setPresetValue(604800)">七天</button>
                        <button class="quick-tag" type="button" @click="setPresetValue(8888888888)">10年</button>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注: </label>
                <div class="layui-input-block">
                    <textarea name="comment" class="layui-textarea"></textarea>

                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    var _this = null,form=null
    Id = urlParam('id')
    new Vue({
        el:"#app",
        data:{
            id:Id,
            disableTime:0,
            serverId:1,
            services: JSON.parse(localStorage.getItem('services')),
        },
        updated(){
            form.render()
        },
        created:function(){
            _this = this;
        },methods: {
            setPresetValue:function(value) {
                console.log("查看对应输入值"+value)
                this.disableTime = value;
            }
        },
        mounted:function() {
            layui.use(['form', 'laydate'], function () {
                form = layui.form
                layer = layui.layer
                $ = layui.$;
                if (Id) {
                    $.get('/admin/silent/' + Id, {}, function (res) {
                        var data = res.data;
                        form.val('example', {
                            "roleId": data.role_id,
                            "disableTime": data.disable_time,
                            "comment": data.comment,
                        })
                        _this.serverId = data.game_area;
                    })
                }
                //监听提交
                form.on('submit(demo1)', function (data) {

                    var formData = data.field;
                    if (Id) { //edit
                        formData._method = 'put';
                        $.post('/admin/silent/' + Id, formData, function (res) {
                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    } else { //add
                        $.post('/admin/silent/add', formData, function (res) {

                            if (res.code === 0) {
                                layer.msg('success', {icon: 6, time: 500}, function () {
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg, {icon: 5})
                            }
                        })
                    }

                    return false;
                });
                form.render();

            })
        }
    });
</script>

</body>
</html>